package com.linkage.jz.bankruptcy.activity.Project.ZQUser;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.activity.Main.CreateAddrActivity;
import com.linkage.jz.bankruptcy.adapter.FullyGridLayoutManager;
import com.linkage.jz.bankruptcy.bean.ZQUserDetailBaseInfo;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.widget.PublicNoticeDialog;
import com.linkage.seapeak.base.BaseWhiteCreateActivity;
import com.linkage.seapeak.pojo.UploadImage;
import com.linkage.seapeak.utils.MenuBottomSheetDialog;
import com.linkage.seapeak.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ChangeCreditorActivity extends BaseWhiteCreateActivity {


    private ZQUserDetailBaseInfo mInfo;
    private String creditorId;

    private int REQUEST_CODE_ADDRESS = 10001;
    PublicNoticeDialog noticeDialog;

    private EditText creditorName;
    private LinearLayout type;
    private TextView typeString;
    private EditText proxyUserName;
    private EditText proxyUserOtherPhone;
    private EditText proxyUserEmail;
    private EditText proxyRegisterNumber;
    private EditText cashName;
    private EditText cashBank;
    private EditText cashInfo;
    private EditText uniformCode;
    private EditText lawPerson;
    private TextView sendAddress;
    private EditText editDetail;
    private TextView tvProjectNote;

    private String bigType;
    private String smallType;

    private RecyclerView recyclerImgsLawPerson;
    private List<UploadImage> imageListLawPerson = new ArrayList<>();
    private GridImagesAdapter imgAdapterLawPerson;

    private RecyclerView recyclerImgsCreditChange;
    private List<UploadImage> imageListCreditChange = new ArrayList<>();
    private GridImagesAdapter imgAdapterCreditChange;

    private RecyclerView recyclerImgsOther;
    private List<UploadImage> imageListOther = new ArrayList<>();
    private GridImagesAdapter imgAdapterOther;

    private RecyclerView recyclerImgsProxyWt;
    private List<UploadImage> imageListProxyWt = new ArrayList<>();
    private GridImagesAdapter imgAdapterProxyWt;

    public static void start(Context context, ZQUserDetailBaseInfo mInfo, String creditorId) {
        Intent intent = new Intent(context, ChangeCreditorActivity.class);
        intent.putExtra("mInfo", mInfo);
        intent.putExtra("creditorId", creditorId);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_creditor, "变更债权人");
        initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_CODE_ADDRESS) {
                sendAddress.setTag(data.getStringExtra("id"));
                sendAddress.setText(data.getStringExtra("desc"));
            }
        }
    }

    @Override
    public void finish() {
        noticeDialog = new PublicNoticeDialog(ChangeCreditorActivity.this, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getId() == R.id.btn_save_notice) {
                    ChangeCreditorActivity.super.finish();
                }
                noticeDialog.dismiss();
            }
        }, 1, "提示", "放弃编辑？",
                "否", "是");
        noticeDialog.show();
    }

    private void initView() {
        mInfo = (ZQUserDetailBaseInfo) getIntent().getSerializableExtra("mInfo");
        creditorId = getIntent().getStringExtra("creditorId");

        if (mInfo.getIdentityBigTypes() != null) {
            bigType = mInfo.getIdentityBigTypes().getValue();
        }
        if (mInfo.getIdentitySmallTypes() != null) {
            smallType = mInfo.getIdentitySmallTypes().getValue();
        }
        addMenu(new MenuAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();
            }
        }, "提交", 0));
        creditorName = (EditText) findViewById(R.id.creditor_name);
        type = (LinearLayout) findViewById(R.id.type);
        typeString = (TextView) findViewById(R.id.type_string);
        proxyUserName = (EditText) findViewById(R.id.proxy_user_name);
        proxyUserOtherPhone = (EditText) findViewById(R.id.proxy_user_other_phone);
        proxyUserEmail = (EditText) findViewById(R.id.proxy_user_email);
        proxyRegisterNumber = (EditText) findViewById(R.id.proxy_register_number);
        cashName = (EditText) findViewById(R.id.cash_name);
        cashBank = (EditText) findViewById(R.id.cash_bank);
        cashInfo = (EditText) findViewById(R.id.cash_info);
        uniformCode = (EditText) findViewById(R.id.uniform_code);
        lawPerson = (EditText) findViewById(R.id.law_person);
        sendAddress = (TextView) findViewById(R.id.send_address);
        editDetail = (EditText) findViewById(R.id.edit_detail);
        tvProjectNote = (TextView) findViewById(R.id.tv_projectNote);

        recyclerImgsLawPerson = (RecyclerView) findViewById(R.id.recycler_imgs_law_person);
        recyclerImgsCreditChange = (RecyclerView) findViewById(R.id.recycler_imgs_credit_change);
        recyclerImgsProxyWt = (RecyclerView) findViewById(R.id.recycler_imgs_proxy_wt);
        recyclerImgsOther = (RecyclerView) findViewById(R.id.recycler_imgs_other);

        String strType = "";
        if (mInfo.getIdentityBigTypes() != null) {
            strType = mInfo.getIdentityBigTypes().getText();
        }

        if (mInfo.getIdentitySmallTypes() != null) {
            strType = strType + "-" + mInfo.getIdentitySmallTypes().getText();
        }

        typeString.setText(strType);
        editDetail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String endStr = editDetail.getText().toString();
                if (endStr.length() > 300) {
                    endStr = endStr.substring(0, 300);
                    editDetail.setText(endStr);
                }
                tvProjectNote.setText(endStr.length() + "/300");
            }
        });

        creditorName.setText(mInfo.getCreditorName());
        type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MenuBottomSheetDialog dialog = new MenuBottomSheetDialog(ChangeCreditorActivity.this);
                dialog.addItem(new MenuBottomSheetDialog.DialogMenuItem("机构-法定代表人", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bigType = "0";
                        smallType = "0";
                        typeString.setText("机构-法定代表人");
                    }
                }));
                dialog.addItem(new MenuBottomSheetDialog.DialogMenuItem("机构-代理人", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bigType = "0";
                        smallType = "1";
                        typeString.setText("机构-代理人");
                    }
                }));
                dialog.addItem(new MenuBottomSheetDialog.DialogMenuItem("自然-本人", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bigType = "1";
                        smallType = "2";
                        typeString.setText("自然-本人");
                    }
                }));
                dialog.addItem(new MenuBottomSheetDialog.DialogMenuItem("自然-代理人", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bigType = "1";
                        smallType = "1";
                        typeString.setText("自然-代理人");
                    }
                }));
                dialog.addItem(new MenuBottomSheetDialog.DialogMenuItem("税务机关", new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        bigType = "2";
                        smallType = "0";
                        typeString.setText("税务机关");
                    }
                }));
            }
        });



        recyclerImgsLawPerson.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                false));
        imgAdapterLawPerson = new GridImagesAdapter(this, TYPE_EDIT);
        imgAdapterLawPerson.setList(imageListLawPerson);
        recyclerImgsLawPerson.setAdapter(imgAdapterLawPerson);

        recyclerImgsCreditChange.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                false));
        imgAdapterCreditChange = new GridImagesAdapter(this, TYPE_EDIT);
        imgAdapterCreditChange.setList(imageListCreditChange);
        recyclerImgsCreditChange.setAdapter(imgAdapterCreditChange);

        recyclerImgsOther.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                false));
        imgAdapterOther = new GridImagesAdapter(this, TYPE_EDIT);
        imgAdapterOther.setList(imageListOther);
        recyclerImgsOther.setAdapter(imgAdapterOther);

        recyclerImgsProxyWt.setLayoutManager(new FullyGridLayoutManager(this, 4, GridLayoutManager.VERTICAL,
                false));
        imgAdapterProxyWt = new GridImagesAdapter(this, TYPE_EDIT);
        imgAdapterProxyWt.setList(imageListProxyWt);
        recyclerImgsProxyWt.setAdapter(imgAdapterProxyWt);

        sendAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ChangeCreditorActivity.this, CreateAddrActivity.class);
                intent.putExtra(CreateAddrActivity.KEY_FROM,CreateAddrActivity.FROM_FLOW);
                startActivityForResult(intent, REQUEST_CODE_ADDRESS);
            }
        });
    }

    /**
     * 保存信息
     */
    private void saveData() {
        if (TextUtils.isEmpty(creditorName.getText().toString())) {
            showToast("债权人姓名不可为空");
            return;
        }
//
//        if (TextUtils.isEmpty(proxyUserPhone.getText().toString())) {
//            showToast("代理人联系方式不可为空");
//            return;
//        }
//
//        if (TextUtils.isEmpty(proxyRegisterNumber.getText().toString())) {
//            showToast("代理人身份证/执业证号码不可为空");
//            return;
//        }
//
//        if (imageListNumber == null || imageListNumber.size() == 0) {
//            showToast("代理人身份证/执业证图片不可为空");
//            return;
//        }
//
//        if (imageListWt == null || imageListWt.size() == 0) {
//            showToast("代理人授权委托书图片不可为空");
//            return;
//        }
//
//        Network.getAPIService()
//                .modifyCreditor(getUserToken(), creditorId, mInfo.getContact(),
//                        bigType, smallType, creditorName.getText().toString(),
//                        mInfo.getProvideBusinessLicense().getValue(), uniformCode.getText().toString(),
//                        lawPerson.getText().toString(),
//                        mInfo.getOtherContact()
//                        )
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new BaseSubscriber<JsonObject>() {
//                    @Override
//                    public void onNext(JsonObject json) {
//                        int code = json.get("code").getAsInt();
//                        if (code != 0) {
//                            showToast("更改失败");
//                            super.onNext(json);
//                            return;
//                        } else {
//                            showToast("更改完成！", ToastUtil.TOAST_TYPE.SUCCESS);
//                        }
//                    }
//
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        showToast("新增失败");
//                    }
//                });
    }

}
